<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>14canvas实现动画的圆</title>
  </head>
  <body>
    <canvas id="canvas" width="400" height="400"></canvas>
    <script>
      var radius = 50
      var increase = true

      const canvas = document.getElementById('canvas')
      const ctx = canvas.getContext('2d')
      // 业务
      function draw() {
        // 清楚给定矩形内的形状
        ctx.clearRect(0, 0, canvas.width, canvas.height)

        ctx.beginPath()
        ctx.arc(100, 100, radius, 0, Math.PI * 2)
        ctx.closePath()
        ctx.fillStyle = '#6528e0'
        ctx.fill()

        if (radius > 100) {
          increase = false
        } else if (radius < 50) {
          increase = true
        }

        if (increase) {
          radius++
        } else {
          radius--
        }
      }

      setInterval(draw, 200)
    </script>
  </body>
</html>
